package team.biocar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.biocar.pojo.Balance;
import team.biocar.service.BalanceService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * @author DeSen Xu
 * @date 2021-11-09 22:50
 */
@RestController
@RequestMapping("/balance")

public class BalanceController {

    // 用setter注入BalanceService
    /**
     *
     */
    private BalanceService balanceService;
            
    @Autowired
    public void setBalanceService(BalanceService balanceService) {
        this.balanceService = balanceService;
    }

    // 根据BalanceService的接口方法, 提供相应的web接口, 返回值使用ResponseEntity


    @GetMapping("/query")

    public ResponseEntity<Balance> queryBalance(@RequestParam String id) {
        // 返回Balance数据
        Balance balance = balanceService.queryBalance(Integer.parseInt(id));
        return ResponseEntity.ok(balance);

    }
    @GetMapping("/sum")

    public ResponseEntity<Integer> getSumOfTheDay(@RequestParam String date) throws ParseException {
        //String转Date
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date1=sdf1.parse(date);

        int i= balanceService.getSumOfTheDay(date1);
        return ResponseEntity.ok(i);
    }

    @GetMapping("/query1")
    public  ResponseEntity<Balance> query(@RequestParam String id){
        Balance query = balanceService.query(Integer.parseInt(id));
        System.out.println(query);
        return ResponseEntity.ok(query);
    }

}
